home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0092_Byte string w-lead zero.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  1KB  |  39 lines

  1. {
  2. For this sort program, I needed a routine to convert a byte value into a
  3. string with leading zeros. So I made one in BASM: Byte2lzStr. If you want,
  4. include this routine in SWAG.
  5. }
  6.  
  7. var s: string;
  8.     tel, n : byte;
  9.  
  10. procedure Byte2lzStr( n, width: byte; var str: string ); assembler;
  11.   { Byte to string with leading zeros }
  12. asm
  13.         std                 { string operations backwards }
  14.         mov   al, [n]       { numeric value to convert    }
  15.         mov   cl, [width]   { width of str                }
  16.         xor   ch, ch        { clear ch                    }
  17.         les   di, str       { adress of str               }
  18.         mov   [di], cl      { length of str               }
  19.         add   di, cx        { start with last char str    }
  20. @start: jcxz  @exit         { done?                       }
  21.         aam                 { divide al by 10             }
  22.         add   al, 30h       { convert remainder to char   }
  23.         stosb               { store digit                 }
  24.         xchg  al, ah        { swap remainder and quotient }
  25.         dec   cl            { count down                  }
  26.         jmp   @start        { next digit                  }
  27. @exit:
  28. end  { Byte2lzStr };
  29.  
  30. begin
  31.   randomize;
  32.   for tel := 1 to 24 do
  33.   begin
  34.     n := random( 256 );
  35.     Byte2lzStr( n, 5, s );
  36.     writeln( tel:2,':  ', n:3,'  ', s,'  [',length(s),']' );
  37.   end;
  38. end.
  39.